package cn.itcast.bos.web.action.trantis;

import cn.itcast.bos.domain.transit.TransitInfo;
import cn.itcast.bos.service.trantis.TrantisInfoService;
import cn.itcast.bos.utils.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

import static com.opensymphony.xwork2.Action.SUCCESS;

@Namespace("/")
@ParentPackage("json-default")
@Scope("protoType")
@Controller
public class TrantisInfoAction extends BaseAction<TransitInfo> {

    private String wayBillIds;
    @Autowired
    private TrantisInfoService trantisInfoService;

    public void setWayBillIds(String wayBillIds) {
        this.wayBillIds = wayBillIds;
    }
    @Action(value = "transit_add",results = {@Result(name="success",type = "json")})
    public String createTransit(){
        System.out.println(wayBillIds);
        Map<String,Object> result = new HashMap<>();
        try {
            trantisInfoService.createTransit(wayBillIds);
            result.put("success",true);
            result.put("msg","转运成功");
        }catch(Exception e){
            e.printStackTrace();
            result.put("success",false);
            result.put("msg","转运失败"+e.getMessage());
        }
        //将结果返回
        ActionContext.getContext().getValueStack().push(result);
        return SUCCESS;
    }
    //分页查询
    @Action(value = "transit_findAll",results = {@Result(name="success",type = "json")})
    public String pageQuery(){
        Pageable pageable = new PageRequest(page-1,rows);
        Page<TransitInfo> pageDate = trantisInfoService.pageQuery(pageable);

        pushPageDataToValueStack(pageDate);
        return SUCCESS;
    }

}
